<template>
  <a-modal v-model:visible="addVisable" :title="title" title-align="start" :ok-text="disabled?'关闭':'保存'" :hide-cancel="disabled" @cancel="handleCancel" @before-ok="handleSubmit" modal-class="formModal" :mask-closable="false">
    <a-form :model="affectForm" ref="affectFormRef" style="max-height: 500px;" :disabled="disabled">
      <a-form-item field="exceptionName" label="异常名称">
        <a-input v-model="affectForm.exceptionName" placeholder="请输入异常名称" />
      </a-form-item>
      <a-form-item field="nodeId" label="部件/分系统">
        <a-select v-model="affectForm.nodeId" placeholder="请选择部件/分系统">
          <a-option v-for="(item) in systemList" :key="item.id" :value="item.id">{{ item.nodeName }}</a-option>
        </a-select>
      </a-form-item>
      <a-form-item field="seriousDegree" label="严重程度">
        <a-select v-model="affectForm.seriousDegree" placeholder="请选择严重程度">
          <a-option v-for="item in seriousDegreeEnum" :key="item.value" :value="item.value">{{ item.label }}</a-option>
        </a-select>
      </a-form-item>
      <a-form-item field="exceptionTime" label="异常时间" v-if="tabVal === 2">
        <a-date-picker v-model="affectForm.exceptionTime" show-time/>
      </a-form-item>
      <a-form-item field="persistenceTime" label="持续时间(分)" v-if="tabVal === 2">
        <a-input v-model="affectForm.persistenceTime" placeholder="请输入持续时间" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { ref, reactive,  defineProps, defineExpose, toRefs } from 'vue'
import { Message } from '@arco-design/web-vue'
import { apiFault } from '@/api/fault';
const props = defineProps({
  systemList: {
    type: Array,
    default: () => []
  },
  getList: {
    type: Function,
    required:true
  }
})
const seriousDegreeEnum = reactive([{
  value: 1,
  label: '轻微'
}, {
  value: 2,
  label: '一般'
}, {
  value: 3,
  label: '严重'
}, {
  value: 4,
  label: '致命'
}])
const {systemList} = toRefs(props)
const addVisable = ref(false)
const title = ref('')
const tabVal = ref('')
const satelliteIdVal = ref('')
const satelliteNameVal = ref('')
const submitType = ref('')
const affectFormRef = ref(null)
const submitEnum = reactive({
  1: apiFault.saveAnticipatoryFailure,
  2: apiFault.saveHistoryFault
})
const editEnum = reactive({
  1: apiFault.editAnticipatoryFailure,
  2: apiFault.editHistoryFault
})
const affectForm = ref({
  id: null,
  nodeId: '',
  exceptionName: '',
  components: '',
  seriousDegree: '',
  persistenceTime: '',
  exceptionTime: ''
})
const disabled = ref(false)
const updateList = () => {
  props.getList(tabVal.value,satelliteIdVal.value)
}
// 查询接口
const getDetailEnum = reactive({
  1: apiFault.selectOneAnticipatoryDetail,
  2: apiFault.selectOneHistoryFaultDetail
})
const openAffectForm = (data, type, tab, satelliteId, satelliteName) => {
  affectForm.value = {
    id: null,
    nodeId: '',
    exceptionName: '',
    components: '',
    seriousDegree: '',
    persistenceTime: '',
    exceptionTime: ''
  }
  if(type === 2){
    disabled.value = true;
  }else{
    disabled.value = false;
  }
  data && getDetailEnum[tab](data).then(res => {
    if(+res.code === 200) {
      affectForm.value = {
        ...res.data
      }
    } else {
      Message.error(res.message)
    }
  })
  title.value = type === 0 ? '新建异常' : type === 1 ? '编辑异常' : '查看异常'
  tabVal.value = tab
  submitType.value = type
  satelliteIdVal.value = satelliteId
  satelliteNameVal.value = satelliteName
  addVisable.value = true
}
const handleCancel = () => {
  addVisable.value = false
  disabled.value = false
}
const handleSubmit = (done) => {
  if(systemList.value.length > 0){
    systemList.value.forEach(item=>{
      if(item.id === affectForm.value.nodeId){
        affectForm.value.components = item.nodeName;
      }
    })
  }
  if (submitType.value === 1) {
    editEnum[tabVal.value]({...affectForm.value}).then(res => {
      if(+res.code === 200) {
        Message.success('编辑成功')
        updateList()
        done()
      } else {
        Message.error(res.message)
      }
      done(false)
    }).catch(() => done(false))
  } else if (submitType.value === 2) {
    done(false);
    addVisable.value = false;
  } else {
    submitEnum[tabVal.value]({...affectForm.value, satelliteId: satelliteIdVal.value, satelliteName: satelliteNameVal.value }).then(res => {
      if(+res.code ===200) {
        done()
        updateList()
        Message.success('新增成功')
      } else {
        Message.error(res.message)
      }
      done(false)
    }).catch(() => done(false))
  }
}
defineExpose({
  openAffectForm
})
</script>

<style lang="less" scoped>

</style>